package org.kernelab.numeric.test;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import org.kernelab.numeric.matrix.Matrix;
import org.kernelab.numeric.matrix.Size;
import org.kernelab.numeric.matrix.gui.MatrixTable;

class LineNumberModel extends AbstractTableModel
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1337589550206203036L;
	TableModel					model;

	public LineNumberModel(TableModel mod)
	{
		this.model = mod;
	}

	public int getColumnCount()
	{
		return 1;
	}

	public String getColumnName(int columnIndex)
	{
		return "";
	}

	public int getRowCount()
	{
		return model.getRowCount();
	}

	public Object getValueAt(int rowIndex, int columnIndex)
	{
		return "";
	}

	public boolean isCellEditable(int row, int column)
	{
		return false;
	}
}

public class LineNumberTable extends JFrame
{
	static {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
		}
	}

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -8104445933524594231L;

	public static void main(String[] args)
	{

		LineNumberTable jl = new LineNumberTable();
		jl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jl.setBounds(100, 100, 600, 400);
		jl.setVisible(true);
	}

	public LineNumberTable()
	{
		Container co = super.getContentPane();
		co.setLayout(new BorderLayout());
		JTable table = new JTable(12, 5);
		JScrollPane sp = new JScrollPane(table);
		RowHeader rowHeader = new RowHeader(table);
		sp.setRowHeaderView(rowHeader);
		sp.getRowHeader().setPreferredSize(new Dimension(35, 10));
		co.add(sp, BorderLayout.CENTER);
	}
}

class RowHeader extends JTable
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 7380807788154594884L;
	MatrixTable<String>			table;

	public RowHeader(JTable table)
	{
		Matrix<String> matrix = new Matrix<String>(new Size(2, 2));

		this.table = new MatrixTable<String>(matrix, "Hey", "Wow");

		// TableColumnModel cm = this.table.getTable().getColumnModel();

		LineNumberModel listModel = new LineNumberModel(table.getModel());
		this.setModel(listModel);

		this.setRowHeight(table.getRowHeight());
		this.getColumn("").setCellRenderer(new RowHeaderRenderer(this.table));

		this.setAutoscrolls(false);
	}
}

class RowHeaderRenderer extends JTableHeader implements TableCellRenderer
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 6977891578565702351L;

	Insets						zeroInsets			= new Insets(0, 0, 0, 0);

	MatrixTable<?>				m;

	RowHeaderRenderer(MatrixTable<?> m)
	{
		super(m.getTable().getColumnModel());

		this.m = m;

		// setOpaque(true);
		// setBorder(UIManager.getBorder("TableHeader.cellBorder"));
		// setHorizontalAlignment(CENTER);
	}

	public Insets getInsets()
	{
		return zeroInsets;
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, final int row, int column)
	{
		this.setToolTipText(String.valueOf(row));
		// JLabel label = new JLabel(String.valueOf(row) + " ",
		// SwingConstants.TRAILING);
		// return label;
		return this;
	}
}
